package cn.sms.basic.service.impl;

import ch.qos.logback.core.net.SyslogOutputStream;
import cn.sms.basic.constant.UserConstants;
import cn.sms.basic.exception.BusinessException;
import cn.sms.basic.service.IVerifycodeService;
import cn.sms.basic.util.SmsUtils;
import cn.sms.basic.util.StrUtils;
import cn.sms.user.domain.User;
import cn.sms.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.TimeUnit;

@Service
public class VerifycodeServiceImpl implements IVerifycodeService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    /*
    * key = "Verifycode:"+phone;
    * value = xxxxxx:时间戳
    */
    @Override
    public void sendSmsCode(String phone,String type){
        if (StringUtils.isEmpty(phone)){
            throw new BusinessException("请输入手机号哟");
        }
        String key = "";
        if("register".equals(type)){
            User user = userMapper.byPhoneQuery(phone);
            if (user != null){
                throw new BusinessException("该用户已存在，请您直接登陆");
            }
            key = UserConstants.PHONE_REGISTER+phone;
        }else if("binder".equals(type)){
            key = UserConstants.PHONE_BINDER+phone;
        }
        sendCode(key);
    }

    private void sendCode(String key) {
        Object value = redisTemplate.opsForValue().get(key);
        String yanzhengma = "";
        if (value != null){
            String[] arr = ((String) value).split(":");
            Long value1 = Long.valueOf(arr[1]);
            if (System.currentTimeMillis()-value1<60*1000){
                throw new BusinessException("请勿重复操作");
            }
            yanzhengma = arr[0];
        }else {
            yanzhengma = StrUtils.getComplexRandomString(6);
        }
        redisTemplate.opsForValue().set(key, yanzhengma+":"+System.currentTimeMillis(), 3, TimeUnit.MINUTES);
        /*SmsUtils.sendSms(phone, " 您的验证码为："+yanzhengma+"，请在三分钟之内使用验证码，验证码请勿透露给他人");*/
        System.out.println("您的验证码为："+yanzhengma+"，请在三分钟之内使用验证码，验证码请勿透露给他人");
    }


    /*@Override
    public void sendSmsCode(String phone){
        if(StringUtils.isEmpty(phone)){
            throw new BusinessException("请输入手机号");
        }
        User user = userMapper.byPhoneQuery(phone);
        if(user!=null){
            throw new BusinessException("该账号已注册，直接登陆即可");
        }
        *//*
         * key = "Verifycode:"+phone;
         * value = xxxxxx:时间戳
         *//*
        String key = "Verifycode:"+phone;
        Object value = redisTemplate.opsForValue().get(key);
        String yanzhengma = "";
        if(value!=null){
            String[] arr = ((String)value).split(":");
            Long value1 = Long.valueOf(arr[1]);
            if (System.currentTimeMillis()-value1<1*60*1000){
                throw new BusinessException("请勿重复点击");
            }
            yanzhengma = arr[0];
        }else {
            yanzhengma = StrUtils.getComplexRandomString(6);
        }
        redisTemplate.opsForValue().set(key, yanzhengma+":"+System.currentTimeMillis(), 3, TimeUnit.MINUTES);
        *//*SmsUtils.sendSms(phone, "您的验证码为:"+yanzhengma+",请在三分钟内使用，请勿透露给他人");*//*
        System.out.println("您的验证码为:"+yanzhengma+",请在三分钟内使用，请勿透露给他人");
    }*/

}
